<?php
namespace app\Tools\Wxchat\WeChatReply;

/**
 * 素材管理
 *
 * Class WechatMaterial
 * @package app\Tools\Wxchat\WeChatReply
 */
class WechatMaterial extends Common {

    //素材列表
    const MATERIAL_LIST_URL = 'https://api.weixin.qq.com/cgi-bin/material/batchget_material?';

    //新增图文
    const MATERIAL_ADD_NEWS_URL = 'https://api.weixin.qq.com/cgi-bin/material/add_news?';

    //修改图文素材
    const MATERIAL_UPDATE_URL = 'https://api.weixin.qq.com/cgi-bin/material/update_news?';

    //删除素材
    const MATERIAL_DEL_URL = 'https://api.weixin.qq.com/cgi-bin/material/del_material?';

    public $access_token;

    public function __construct()
    {
        $this->access_token = $this->get_access_token();
    }


    /**
     * 获取素材列表
     *
     * @param string $type
     * @return mixed
     */
    public function getList($type = 'news')
    {
        $url = self::MATERIAL_LIST_URL . 'access_token=' . $this->access_token;
        $data = [
            'type' => $type,
            'offset' => 0,
            'count' => 20,
        ];

        $res = $this->httpRequest($url,'POST',$data);
        return $res;
    }


    public function addNews()
    {

    }



    /*
     * 修改图文素材
     *
     * data格式
     * {
        "media_id":MEDIA_ID,
        "index":INDEX,
        "articles": [
            {
                "title": TITLE,
                "thumb_media_id": THUMB_MEDIA_ID,
                "author": AUTHOR,
                "digest": DIGEST,
                "show_cover_pic": SHOW_COVER_PIC(0 / 1),
                "content": CONTENT,
                "content_source_url": CONTENT_SOURCE_URL
            },
            //若新增的是多图文素材，则此处应还有几段articles结构
        ]
        }
    */
    public function updateNews($data)
    {
        $url = self::MATERIAL_UPDATE_URL . 'access_token=' . $this->access_token;
        $res = $this->httpRequest($url,'POST',$data);
        return $res;
    }


    /**
     * 删除素材
     *
     * @param $mediaId
     * @return mixed
     */
    public function del($mediaId)
    {
        $url = self::MATERIAL_DEL_URL . 'access_token=' . $this->access_token;
        $data = ['media_id'=>$mediaId];
        $res = $this->httpRequest($url,'POST',$data);
        return $res;
    }

}